using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Diffluxum.CCScoring
{
    public class CCScoringXmlFormatter
    {
        public void FormatXml(XmlWriter writer, IEnumerable<IUserScorer> userScorers)
        {
            writer.WriteStartElement("UserScores");
            foreach (IUserScorer userScorer in userScorers)
            {
                WriteUserScorer(writer, userScorer);
            }

            writer.WriteEndElement();

        }

        private void WriteUserScorer(XmlWriter writer, IUserScorer userScorer)
        {
            writer.WriteStartElement("UserScore");
            writer.WriteAttributeString("name", userScorer.UserName);
            writer.WriteAttributeString("points", userScorer.TotalPoints.ToString());

            writer.WriteStartElement("Grades");
            WriteGrades(writer, userScorer);
            writer.WriteEndElement();

            writer.WriteElementString("RedToGreen", userScorer.RedToGreenBuilds.ToString());
            writer.WriteElementString("GreenToGreen", userScorer.GreenToGreenBuilds.ToString());
            // Red to red not implemented
            //writer.WriteElementString("RedToRed", userScorer.RedToRedBuilds.ToString());
            writer.WriteElementString("GreenToRed", userScorer.GreenToRedBuilds.ToString());




            writer.WriteStartElement("CausedBrokenBuildSpans");
            writer.WriteAttributeString("TotalTime", userScorer.TotalCausedBreakingTime.ToString());
            WriteBrokenSpans(writer, userScorer.CausedBreakSpans);
            writer.WriteEndElement();

            writer.WriteStartElement("CheckedInToBrokenBuildSpans");
            writer.WriteAttributeString("TotalInvolvedTime", userScorer.TotalSubmittedToBreakingTime.ToString());
            WriteBrokenSpans(writer, userScorer.CheckedInToBreakSpans);
            writer.WriteEndElement();


            writer.WriteEndElement();

        }

        private void WriteBrokenSpans(XmlWriter writer, IList<IBrokenBuildsSpan> spans)
        {
            foreach (IBrokenBuildsSpan span in spans)
            {
                writer.WriteStartElement("BrokenBuildsSpan");
                writer.WriteAttributeString("brokenTime", span.TotalBreakTime.ToString());

                foreach (BuildResult build in span.IncludedBuilds)
                {
                    WriteBuild(writer, build);
                }
                writer.WriteEndElement();
            }
        }

        private void WriteBuild(XmlWriter writer, BuildResult build)
        {
            writer.WriteStartElement("Build");
            writer.WriteAttributeString("buildDate", build.BuildDate.ToString());
            writer.WriteAttributeString("buildSuccess", build.BuildSuccess.ToString());

            writer.WriteStartElement("ResponsibleUsers");
            foreach (string userName in build.ResponsibleUsers)
            {
                writer.WriteElementString("User", userName);
            }

            writer.WriteEndElement();

            writer.WriteEndElement();

        }

        private void WriteGrades(XmlWriter writer, IUserScorer userScorer)
        {
            foreach (CCGrade grade in userScorer.Grades)
            {
                writer.WriteStartElement("Grade");
                writer.WriteAttributeString("points", grade.Points.ToString());
                writer.WriteAttributeString("reason", grade.Reason);
                writer.WriteEndElement();
            }
        }

    }
}
